package com.itheima.demo;

import com.itheima.domain.Student;
import com.itheima.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class TestDemo01 {
    //    将流对象 等定义为全局变量 可在全局使用
    private InputStream is = null;
    private SqlSession sqlSession = null;
    SqlSessionFactory factory = null;
    private UserMapper mapper = null;
    @Before
    public  void before() throws IOException {
        // 读取核心配置文件
        is = Resources.getResourceAsStream("sql.xml");
        // 构建工厂对象 通过抽象类的构造者SqlSessionFactoryBuilder()的build方法
        factory = new SqlSessionFactoryBuilder().build(is);
        // 通过factory对象获取 SqlSession的对象
        sqlSession = factory.openSession();
        // 通过sqlSession获取 UserMapper代理对象
         mapper = sqlSession.getMapper(UserMapper.class);
    }
    @After
    public  void after() throws IOException {
        //6、事务提交
        sqlSession.commit();
        //7、释放资源
        sqlSession.close();
        is.close();
    }
    @Test
    public  void testFindAll(){
        List<Student> list = mapper.findAll();
        System.out.println(list);
    }

    @Test
    public void find1() {
        List<Student> byName = mapper.findByName(" ");
        System.out.println(byName);
    }
    @Test
    public void find2() {
        List list1=new ArrayList();
        list1.add(1);
        List<Student> byName = mapper.findByName1(list1);
        System.out.println(byName);
    }
    @Test
    public void find3() {
        String[] strs={"a"};
        List<Student> byName = mapper.findByName2(strs);
        System.out.println(byName);
    }
    @Test
    public  void  delete() {
        //组装多个id的数组
      Integer[] ids={3,4,5};
       /*for (Integer id : ids) {
            mapper.deleteById(id);
        }*/
    mapper.deleteByIds(ids);

    }
}
